package com.honey.standard.writer.io;

import java.io.File;

/**
 * 验证文件存储,文件验证通过覆盖文件,如果验证失败使用备份的方式生成文件.
 * @author Administrator
 *
 */
class CheckingAndBackupStorage extends Storage {

	public CheckingAndBackupStorage(File source, boolean checked) {
		super(source, checked);
	}

	public CheckingAndBackupStorage(File source, String charset, boolean checked) {
		super(source, charset, checked);
	}
	
	public CheckingAndBackupStorage(String source, String charset, boolean checked) {
		super(source, charset, checked);
	}

	@Override
	protected void setSource(File source) {
		if( !checked ){
			source = backupFile(source);
			super.setSource(source);
		}else{
			super.setSource(source);
		}	
	}
	private File backupFile(File source) {
		File answer = null;
		if( source.exists() ){
			File backup = IoUtility.uniqueFile( source.getParentFile(), source.getName() );
			IoUtility.copyFile(source, backup);
			answer = source;
		}else{
			answer = source;
		}
		return answer ;
	}
	
	@Override
	public boolean supportWrite() {
		return true;
	}
}
